package com.albert.eurekaproducer.controller;

import com.albert.eurekaproducer.config.AccessToken;
import com.albert.eurekaproducer.model.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
@RequestMapping(value = "/userinfo")
public class UserInfoController {
    private final static Logger logger = LoggerFactory.getLogger(UserInfoController.class);

    @AccessToken
    @RequestMapping(value = "/add")
    public String addUser(@RequestBody UserInfo userInfo) {
        try {
            logger.info("add user api receive msg {} ", userInfo);
            return "save success";
        } catch (Exception e) {
            logger.error("add user api exception ,e={} , ", e.getMessage(), e);
        }
        return "";
    }

    @AccessToken
    @RequestMapping(value = "/query_user")
    public UserInfo userQuery(@RequestBody String loginName) {
        try {
            logger.info("query_user api receive msg {} ", loginName);
            Random random = new Random();
            int n = random.nextInt(2);
            if (n < 1) {
                UserInfo userInfo = new UserInfo();
                userInfo.setLoginName("zlf");
                userInfo.setId(1);
                return userInfo;
            } else {
                return null;
            }
        } catch (Exception e) {
            logger.error("query_user user api exception ,e={} , ", e.getMessage(), e);
        }
        return null;
    }
}
